package com.ajocer.springbootinit.controller;

import com.ajocer.springbootinit.common.BaseResponse;
import com.ajocer.springbootinit.common.ErrorCode;
import com.ajocer.springbootinit.common.ResultUtils;
import com.ajocer.springbootinit.exception.ThrowUtils;
import com.ajocer.springbootinit.model.dto.tourismrag.TourismRagRequest;
import com.ajocer.springbootinit.rag.TourismAgent;
import com.ajocer.springbootinit.rag.TourismRagService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 旅游推荐RAG控制器
 */
@RestController
@RequestMapping("/tourismRag")
@Slf4j
public class TourismRagController {

    @Autowired
    private TourismRagService tourismRagService;

    @Autowired
    private TourismAgent tourismAgent;

    /**
     * 推荐景点
     * @param tourismRagRequest 请求
     * @return 推荐结果
     */
    @PostMapping("/recommendSpots")
    public BaseResponse<String> recommendSpots(@RequestBody TourismRagRequest tourismRagRequest, HttpServletRequest request) {
        if (tourismRagRequest == null || StringUtils.isBlank(tourismRagRequest.getUserPreference())) {
            throw new RuntimeException(ErrorCode.PARAMS_ERROR.getMessage());
        }
        String userPreference = tourismRagRequest.getUserPreference();
        String result = tourismRagService.recommendSpots(userPreference);
        return ResultUtils.success(result);
    }

    /**
     * 推荐路线
     * @param tourismRagRequest 请求
     * @return 推荐结果
     */
    @PostMapping("/recommendRoutes")
    public BaseResponse<String> recommendRoutes(@RequestBody TourismRagRequest tourismRagRequest, HttpServletRequest request) {
        if (tourismRagRequest == null || StringUtils.isBlank(tourismRagRequest.getUserPreference())) {
            throw new RuntimeException(ErrorCode.PARAMS_ERROR.getMessage());
        }
        String userPreference = tourismRagRequest.getUserPreference();
        String result = tourismRagService.recommendRoutes(userPreference);
        return ResultUtils.success(result);
    }

    /**
     * Agent执行任务
     * @param tourismRagRequest 请求
     * @return 执行结果
     */
    @PostMapping("/executeTask")
    public BaseResponse<String> executeTask(@RequestBody TourismRagRequest tourismRagRequest, HttpServletRequest request) {
        if (tourismRagRequest == null || StringUtils.isBlank(tourismRagRequest.getUserPreference())) {
            throw new RuntimeException(ErrorCode.PARAMS_ERROR.getMessage());
        }
        String userQuery = tourismRagRequest.getUserPreference();
        String result = tourismAgent.executeTask(userQuery);
        return ResultUtils.success(result);
    }
} 